/**
 * Main360.java created by zhangzhidong 
 * at 下午8:18:29 2017年8月26日
 */
package algorithm;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author zhangzhidong<br>
 * comment generated at 2017年8月26日下午8:18:29<br>
 * 
 */
public class Main360 {
	public static void main(String[] args) {
		long arr1[] = new long[]{2,34,6,7,2,7};
		long arr2[] = new long[]{2,5,7,4,3,78};
		System.out.println(get(arr1,arr2));
	}
	static  int idx1[] = new int[2];
	static  int idx2[] = new int[2];
static 	long get(long arr1[],long arr2[]){
	long sumAbs = Integer.MAX_VALUE;
	//不交换

	long sum1 = 0,sum2 = 0;
	for(long t:arr1) sum1+=t;
	for(long t:arr2) sum2+=t;

	//不交换
	sumAbs = Math.abs(sum1-sum2);
		solve(arr1,arr2,sumAbs);
		return sumAbs;
	}
static long solve(long arr1[],long arr2[],long sumAbs){
	for(int i = 0 ;i<arr1.length;i++){
		for(int j=0;j<arr2.length;j++){
			if(arr1[i] == arr2[j]){
				continue;
			}else{
				swap(arr1,arr2,i,j);
//				long sumSwap2 = solve(arr1,arr2,sumAbs);
				long tempsum1 = 0,tempsum2 = 0;
				for(long t:arr1) tempsum1+=t;
				for(long t:arr2) tempsum2+=t;
				if(Math.abs(tempsum2-tempsum1)<sumAbs) {
					idx1[0]=i;
					idx2[0]=j;
					sumAbs = Math.abs(tempsum2-tempsum1);
				}
				swap(arr1,arr2,i,j);
			}
		}
	}
	return sumAbs;
}
	static void swap(long arr1[],long arr2[],int k,int j){
		long temp = arr1[k];
		arr1[k] = arr2[j];
		arr2[j]=temp;
	}
}
